package com.mengmeng.quicklymessage;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;
import android.util.Log;

public class DirectMessageWidget extends BaseWidget {
    private final String TAG = "DirectMessageWidght";

    private final String ACTION_SEND_MESSAGE = "1";
    private final String SENTSMS_ACTION = "SENT_SMS_ACTION";

    private int number;

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.v(TAG, "onEnabled");
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.v(TAG, "onUpdate");

        // RemoteViews rv = new RemoteViews(context.getPackageName(),
        // R.layout.direct_message_widght);
        // Intent intentClick = new Intent(ACTION_SEND_MESSAGE);
        // intentClick.setClass(context, DirectMessageWidget.class);
        // PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
        // intentClick, 0);
        // rv.setOnClickPendingIntent(R.id.direct_message_button,
        // pendingIntent);
        // appWidgetManager.updateAppWidget(appWidgetIds, rv);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.v("111", "receive");
        // if (intent.getAction().equals(ACTION_SEND_MESSAGE)) {
        // Toast.makeText(context, "1111", Toast.LENGTH_SHORT).show();
        // // Intent messageIntent = new Intent(SENTSMS_ACTION);
        // // PendingIntent pi = PendingIntent.getBroadcast(context, 0,
        // // messageIntent, 0);
        //
        // if (number == 0) {
        // startActivity(context, intent);
        // } else {
        // sendMessage();
        // }
        // }
        startActivity(context, intent);
    }

    private void startActivity(Context context, Intent intent) {
        final Uri dataUri = intent.getData();
        final Intent newIntent = new Intent(
                "com.android.contacts.action.QUICK_CONTACT");
        newIntent.setSourceBounds(intent.getSourceBounds());
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        newIntent.setData(dataUri);
        context.startActivity(newIntent);
    }

    private void sendMessage() {
        SmsManager smsMgr = SmsManager.getDefault();
        smsMgr.sendTextMessage("+8613811394669", null, "我走拉。用app发的。。", null,
                null);
    }

    // class sendReceiver extends BroadcastReceiver {
    // // 写个接收器
    // @Override
    // public void onReceive(Context context, Intent intent) {
    // // TODO Auto-generated method stub
    // int resultCode = getResultCode();
    // if (resultCode == Activity.RESULT_OK) {
    // Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
    // } else {
    // Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
    // System.out.println("发送失败");
    // }
    // }
    // }

}
